﻿using System;

using Microsoft.SharePoint.Client;

using SBPweb.RIA.Client.Controls.Base;

using SBPweb.Sharepoint.Client.Controls.Lists;

namespace SBPweb.Sharepoint.Client.Controls
{

    public partial class Page : UserControlBase
    {

        public Page()
            : base()
        {
            InitializeComponent();
        }

        public ClientContext SharepointContext
        {
            get;
            private set;
        }

        public void InitializeContext()
        {
            if (ClientContext.Current == null)
            {
                if (!System.Diagnostics.Debugger.IsAttached)
                {
                    string uri = Application.PageUri.OriginalString;
                    uri = uri.Substring(0, uri.LastIndexOf('/'));
                    uri = uri.Substring(0, uri.LastIndexOf('/'));
                    uri = uri.Substring(0, uri.LastIndexOf('/'));
                    SharepointContext = new ClientContext(Application.PageUri);
                }
                else
                {
                    SharepointContext = new ClientContext(string.Format("{0}://{1}/", Application.PageUri.Scheme, "sps01/sandbox"));
                    //SharepointContext = new ClientContext(string.Format("{0}://{1}/", Application.PageUri.Scheme, "cerberus"));
                }
                SharepointContext.Load(SharepointContext.Site);
                SharepointContext.Load(SharepointContext.Web);
                SharepointContext.ExecuteQueryAsync(OnContextInitialize, OnContextFailure);
            }
            else
            {
                SharepointContext = ClientContext.Current;
                OnContextInitialize(this, null);
            }
        }

        public void InitializeContent()
        {
            string present = ApplicationInitialParameters.Get<string>("present");
            if (string.IsNullOrEmpty(present))
            {
                throw new ArgumentNullException("The Initial parameter called 'present' was not set!");
            }
            switch (present.ToLowerInvariant())
            {
                default:
                    break;
                case "list":
                    ListGrid lg = new ListGrid();
                    string listName = ApplicationInitialParameters.Get<string>("listName");
                    if (string.IsNullOrEmpty(listName))
                    {
                        throw new ArgumentNullException("The Initial parameter called 'listName' was not set!");
                    }
                    lg.ListName = listName;
                    lg.InitializeContext(SharepointContext);
                    Content = lg;
                    break;
            }
        }
        
        protected virtual void OnContextInitialize(object sender, ClientRequestSucceededEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                InitializeContent();
            });
        }

        protected virtual void OnContextFailure(object sender, ClientRequestFailedEventArgs e)
        {
            if (e.Exception != null)
            {
                throw e.Exception;
            }
        }  

    }

}